package com.pipayshop.controller;

import com.pipayshop.domain.vo.PageDataVO;
import com.pipayshop.service.PhysicalStoreFollowFocusService;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 粉丝关注表 前端控制器
 * </p>
 *
 * @author jjf
 * @since 2023-07-29
 */
@RestController
@Api(value = "实体店关注接口",tags = "实体店关注接口")
@RequestMapping("/pipayshopapi/store-follow-focus")
public class PhysicalStoreFollowFocusController {
    @Resource
    private PhysicalStoreFollowFocusService followFocusService;


    @GetMapping("userFollowItem/{followId}/{shopId}")
    @ApiOperation("关注实体店")
    public ResponseVO userFollowItem(@PathVariable String followId, @PathVariable String shopId) {
        try {
            Boolean update = followFocusService.userFollowItem(followId, shopId);
            if (! update ){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("关注实体店成功");
        } catch (Exception e) {
            throw new BusinessException("follow_online_store_failed");
        }
    }

    @PostMapping("userUnfollow/{followId}/{shopId}")
    @ApiOperation("用户取消关注")
    private ResponseVO userUnfollow(@PathVariable String followId,@PathVariable String shopId){
        try {
            Boolean result = followFocusService.userUnfollow(followId, shopId);
            if (!result){
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("用户取消关注成功");
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("cancel_follow_failed");
        }
    }

    @GetMapping("getFollowList/{shopId}/{pageNum}/{pageSize}")
    @ApiOperation("获取实体店的粉丝列表")
    public ResponseVO<PageDataVO> getFollowList(@PathVariable String shopId,
                                                @ApiParam("当前页")
                                    @PathVariable Integer pageNum,
                                                @ApiParam("展示条数")
                                    @PathVariable Integer pageSize){
        try{
            PageDataVO result = followFocusService.getFollowList(shopId,pageNum,pageSize);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("fans_list_retrieval_failed");
        }
    }

    /**
     * 校验是否已经关注该店铺
     */
    @GetMapping("isFollowShop/{shopId}/{userId}")
    @ApiOperation("校验是否已经关注该店铺")
    public ResponseVO<Boolean> isFollowShop(@PathVariable String shopId, @PathVariable String userId){
        try{
            boolean result = followFocusService.isFollowShop(shopId,userId);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("check_if_followed_store_failed");
        }
    }

    /**
     * 根据shopId查找店铺的粉丝数量
     */
    @PostMapping("SelectFollowShopSum/{shopId}")
    @ApiOperation("根据shopId查找店铺的粉丝数量")
    public ResponseVO<Integer> SelectFollowShopSum(@PathVariable String shopId){
        try{
            Integer result = followFocusService.SelectFollowShopSum(shopId);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("find_store_fans_count_failed");
        }
    }
    @GetMapping("followList/{page}/{limit}/{userId}")
    @ApiOperation("根据用户id-查询-关注-实体店列表")
    public ResponseVO<PageDataVO> followList(@PathVariable Integer page,@PathVariable Integer limit,@PathVariable("userId") String userId) {
        try {
            PageDataVO followList = followFocusService.getFollowList(page, limit, userId);
            return ResponseVO.getSuccessResponseVo(followList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("查询失败");
        }
    }
}
